package one.stand.controller;

import com.jmp.api.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CompanyEditRequest;
import com.jmp.base.dto.*;
import com.jmp.feign.CompanyFeign;
import com.jmp.feign.WebsiteFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.mapper.IndustryMapper;
import one.stand.model.*;
import one.stand.request.CompanyRecruitRequest;
import one.stand.request.CompanyRequest;
import one.stand.request.CompanyVideoRequest;
import one.stand.request.WebsiteRequest;
import one.stand.response.CompanyResponse;
import one.stand.response.DemandResponse;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.service.CompanySelectService;
import one.stand.service.WebsiteService;
import one.stand.util.AssertUtil;
import one.stand.util.OneConstant;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author Administrator
 */
@RequestMapping("crmApi/company")
@RestController
@Api(description = "企业模块")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyFeign companyFeign;
    @Autowired
    private WebsiteFeign websiteFeign;

    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private IndustryMapper industryMapper;
    @Autowired
    private CompanySelectService companySelectService;
    @Autowired
    private WebsiteService websiteService;

    @ApiOperation(value = "添加企业")
    @PostMapping("info")
    @SecurityAuthCheck
    public ResultModel<CompanyInfoResponse> info(@RequestAttribute CompanyInfoRequest request) {
        Company company = companyBaseService.getById(request.getCompanyId());
        CompanyInfoResponse response = CompanyInfoResponse.builder()
                .companyLogo(company.getCompanyLogo())
                .companyName(company.getCompanyName())
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "个人企业信息")
    @PostMapping("getCompany")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<CompanyDetailResponse> getCompany(@RequestAttribute CompanyDetailRequest request) {
        Integer companyId = request.getCompanyId() != null ? request.getCompanyId() : request.getCId();
        Company company = companyBaseService.getById(companyId);
        AssertUtil.nonNull(company, ResultEnum.NO_DATA);
        AssertUtil.isTrue(OneConstant.ZERO.equals(company.getIsDel()), ResultEnum.NO_DATA);

        String name = null;
        if (request.getMyCardId() != null) {
            BusinessCard businessCardModel = businessCardBaseService.getCustomer(request.getMyCardId());
            name = businessCardModel.getName();
        }

        IndustryModel industryModel = industryMapper.selectByPrimaryKey(company.getIndustryId());

        boolean selectCompany = companySelectService.isSelect(companyId);

        CompanyDetailResponse response = CompanyDetailResponse.builder()
                .companyId(companyId)
                .companyName(company.getCompanyName())
                .userId(company.getUserId())
                .companyLogo(company.getCompanyLogo())
                .companyNotice(company.getCompanyNotice())
                .password(company.getPassword())
                .industryId(company.getIndustryId())
                .industryName(Objects.nonNull(industryModel) ? industryModel.getIndustryName() : null)
                .name(name)
                .selectCompany(selectCompany)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "查询公司信息(中转)")
    @PostMapping("getCompanyTransfer")
    @SecurityAuthCheck
    public ResultModel<CompanyResponse> getCompanyTransfer(@RequestAttribute CompanyRequest request) {

        return crmFegin.getCompany(request);
    }

    @ApiOperation(value = "得到登录人的公司id")
    @PostMapping("getCompanyId")
    @SecurityAuthCheck
    public ResultModel<CompanyResponse> getCompanyId(@RequestAttribute CompanyRequest request) {

        return crmFegin.getCompanyId(request);
    }

    @ApiOperation(value = "查询公司信息(用于下拉列表查询)")
    @ApiImplicitParam(name = "companyName", value = "公司名称", paramType = "String")
    @PostMapping("getCompanyAllInfo")
    @SecurityAuthCheck
    public ResultModel getCompanyAllInfo(@RequestAttribute CompanyRequest request) {
        return crmFegin.getCompanyAllInfo(request);
    }

    @ApiOperation(value = "企业邀请")
    @ApiImplicitParam(name = "companyId", value = "企业id", paramType = "String")
    @PostMapping("invitation")
    public ResultModel<CompanyResponse> invitation(CompanyRequest request) {

        return crmFegin.invitation(request);
    }

    @ApiOperation(value = "修改企业")
    @PostMapping("updCompany")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<Boolean> updCompany(@RequestAttribute com.jmp.api.dto.CompanyEditRequest request) {
        final RequestContext.User user = RequestContextHolder.getRequestContext().getUser();

        CompanyEditRequest companyEditRequest = CompanyEditRequest.builder()
                .companyId(user.getCompanyId())
                .companyLogo(request.getCompanyLogo())
                .companyName(request.getCompanyName())
                .password(request.getPassword())
                .industryId(request.getIndustryId())
                .companyNotice(request.getCompanyNotice())
                .phone(request.getPhone())
                .build();
        ResultModel resultModel = companyFeign.edit(companyEditRequest);
        AssertUtil.isTrue(resultModel);

        return ResultModel.success(true);
    }

    @ApiOperation(value = "查询公司官网信息")
    @PostMapping("selectWebsiteInfo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<WebsiteResponse> selectWebsiteInfo() {
        final Integer companyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();
        // 查询公司官网
        Website website = websiteService.getByCompanyId(companyId);
        Company company = companyBaseService.getById(companyId);
        WebsiteDemandDto demandDto = WebsiteDemandDto.builder().companyId(companyId).build();
        demandDto.setPageNum(1);
        demandDto.setPageSize(1);
        ResultModel<WebsiteDemandVo> demandVoResultModel = websiteFeign.demand(demandDto);
        AssertUtil.isTrue(demandVoResultModel);
        Long num = demandVoResultModel.getData().getDemandPage().getTotal();

        WebsiteResponse websiteResponse = WebsiteResponse.builder()
                .websiteId(website.getWebsiteId())
                .companyId(website.getCompanyId())
                .photos(website.getPhotos())
                .companyIntroduce(website.getCompanyIntroduce())
                .goodsIntroduce(website.getGoodsIntroduce())
                .videoUrl(website.getVideoUrl())
                .phone(website.getPhone())
                .address(website.getAddress())
                .createTime(website.getCreateTime())
                .updateTime(website.getUpdateTime())
                .isDel(website.getIsDel())
                .companyName(company.getCompanyName())
                .companyLogo(company.getCompanyLogo())
                .companyNotice(company.getCompanyNotice())
                .industryId(company.getIndustryId())
                .companyPhone(company.getPhone())
                .recruitPhone(company.getRecruitPhone())
                .num(num.intValue())
                .build();
        // 查询公司视频
        ResultModel<WebsiteVideoVo> resultModel = websiteFeign.video(WebsiteVideoDto.builder().companyId(companyId).build());
        AssertUtil.isTrue(resultModel);
        websiteResponse.setCompanyVideoModelList(ConvertUtils.list(resultModel.getData().getVideoList(), video -> {
            return WebsiteResponse.Video.builder()
                    .videoId(video.getVideoId())
                    .url(video.getUrl())
                    .describes(video.getDescribes())
                    .cover(video.getCover())
                    .sort(video.getSort())
                    .build();
        }));
        return ResultModel.success(websiteResponse);
    }

    @ApiOperation(value = "查询公司官网表单提交信息")
    @PostMapping("selectDemand")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel selectDemand(@RequestAttribute PageRequest request) {
        final Integer optCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();
        WebsiteDemandDto demandDto = WebsiteDemandDto.builder().companyId(optCompanyId).build();
        demandDto.setPageNum(request.getPageNum());
        demandDto.setPageSize(request.getPageSize());
        ResultModel<WebsiteDemandVo> resultModel = websiteFeign.demand(demandDto);
        AssertUtil.isTrue(resultModel);

        PageResponse2<DemandResponse> response = ConvertUtils.page2(resultModel.getData().getDemandPage(), demand -> {
            DemandResponse demandResponse = new DemandResponse();
            demandResponse.setLog(demand.getLog());
            demandResponse.setName(demand.getName());
            demandResponse.setPhone(demand.getPhone());
            demandResponse.setText(demand.getText());
            return demandResponse;
        });
        return ResultModel.success(response);
    }

    @ApiOperation(value = "添加/修改公司官网信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "websiteId", value = "官网id", paramType = "Integer"),
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "photos", value = "官网图片(轮播图，逗号隔开)", paramType = "String"),
            @ApiImplicitParam(name = "companyIntroduce", value = "公司介绍", paramType = "String"),
            @ApiImplicitParam(name = "goodsIntroduce", value = "产品介绍", paramType = "String"),
            @ApiImplicitParam(name = "videoUrl", value = "视频介绍地址", paramType = "String"),
            @ApiImplicitParam(name = "phone", value = "公司电话", paramType = "String"),
            @ApiImplicitParam(name = "address", value = "公司地址", paramType = "String"),
    })
    @PostMapping("addWebsiteInfo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel addWebsiteInfo(@RequestAttribute WebsiteRequest request) {

        return crmFegin.addWebsiteInfo(request);
    }

    @ApiOperation(value = "添加公司視頻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "公司视频id", paramType = "Integer"),
            @ApiImplicitParam(name = "url", value = "公司视频路径", required = true, paramType = "String"),
            @ApiImplicitParam(name = "describes", value = "公司视频描述", required = true, paramType = "String"),
            @ApiImplicitParam(name = "cover", value = "封面", required = true, paramType = "String"),
    })
    @PostMapping("insertCompanyVideo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<Boolean> insertCompanyVideo(@RequestAttribute CompanyVideoRequest request) {
        final Integer companyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();
        ResultModel resultModel = websiteFeign.videoSave(WebsiteVideoSaveDto.builder()
                .videoId(request.getVideoId())
                .companyId(companyId)
                .url(request.getUrl())
                .describes(request.getDescribes())
                .cover(request.getCover())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success(true);
    }

    @ApiOperation(value = "删除公司視頻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "公司视频id", required = true, paramType = "Integer"),
    })
    @PostMapping("delCompanyVideo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<Boolean> delCompanyVideo(@RequestAttribute CompanyVideoRequest request) {
        ResultModel resultModel = websiteFeign.videoRemove(WebsiteVideoRemoveDto.builder()
                .videoId(request.getVideoId())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success(true);
    }

    @ApiOperation(value = "上下移动公司视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "公司视频id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "type", value = "1上移，2下移", required = true, paramType = "Integer"),
    })
    @PostMapping("moveCompanyVideo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<Boolean> moveCompanyVideo(@RequestAttribute CompanyVideoRequest request) {
        ResultModel resultModel = websiteFeign.videoMove(WebsiteVideoMoveDto.builder()
                .videoId(request.getVideoId())
                .isUp(request.getType() == 1)
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success(true);
    }

    @ApiOperation(value = "查询公司招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
    })
    @PostMapping("selectCompanyRecruit")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel selectCompanyRecruit(@RequestAttribute CompanyRecruitRequest request) {

        return crmFegin.selectCompanyRecruit(request);
    }

    @ApiOperation(value = "添加/修改公司招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitId", value = "招聘id", paramType = "Integer"),
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "position", value = "职位", paramType = "String"),
            @ApiImplicitParam(name = "recruitAddress", value = "招聘地址", paramType = "String"),
            @ApiImplicitParam(name = "experience", value = "1:应届，2:1年内,3:1-3年,4:3-5年,5:5-10年,6:10年以上,7:全部", paramType = "Integer"),
            @ApiImplicitParam(name = "education", value = "1:全部,2:初中以下,3:中专/中技,4:高中,5:大专,6:本科,7:硕士,8:博士", paramType = "Integer"),
            @ApiImplicitParam(name = "maxSalary", value = "最大薪资", paramType = "Integer"),
            @ApiImplicitParam(name = "minSalary", value = "最小薪资", paramType = "Integer"),
            @ApiImplicitParam(name = "requirement", value = "要求", paramType = "String"),
            @ApiImplicitParam(name = "state", value = "1:招聘中,2:招聘结束", paramType = "Integer"),
            @ApiImplicitParam(name = "accuracy", value = "精度", paramType = "String"),
            @ApiImplicitParam(name = "dimension", value = "维度", paramType = "String"),
    })
    @PostMapping("addCompanyRecruit")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel addCompanyRecruit(@RequestAttribute CompanyRecruitRequest request) {

        return crmFegin.addCompanyRecruit(request);
    }

    @ApiOperation(value = "添加招聘HR电话")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "recruitPhone", value = "企业招聘电话", required = true, paramType = "String"),
    })
    @PostMapping("addHrPhone")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel addHrPhone(@RequestAttribute CompanyRequest request) {

        return crmFegin.addHrPhone(request);
    }

    @ApiOperation(value = "删除公司招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitId", value = "企业招聘id", required = true, paramType = "Integer"),
    })
    @PostMapping("delCompanyRecruit")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel delCompanyRecruit(@RequestAttribute CompanyRecruitRequest request) {

        return crmFegin.delCompanyRecruit(request);
    }

}
